/*--------------------------------*- C++ -*----------------------------------*\
| =========                 |                                                 |
| \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
|  \\    /   O peration     | Version:  v2306                                 |
|   \\  /    A nd           | Website:  www.openfoam.com                      |
|    \\/     M anipulation  |                                                 |
\*---------------------------------------------------------------------------*/
#include "fvCFD.H"
#include "volFields.H"
#include "OFstream.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

int main(int argc, char *argv[])
{
    #include "setRootCase.H"
    #include "createTime.H"
    #include "createMesh.H"

    // 选择参考方向（x=0, y=1, z=2）
    const direction refAxis = 0; // 默认x方向，按需修改
    vector refDir(Zero);
    refDir[refAxis] = 1.0;

    // 创建输出文件
    OFstream outputFile("faceAngles.csv");
    outputFile << "PatchName,FaceIndex,Angle(deg)" << endl;

    // 遍历所有边界Patch
    const polyBoundaryMesh& patches = mesh.boundaryMesh();
    
    forAll(patches, patchI)
    {
        const polyPatch& patch = patches[patchI];
        
        if (patch.size() > 0)
        {
            Info<< "\nProcessing patch: " << patch.name() << nl;

            // 获取面法向矢量（已单位化）
            const vectorField& faceNormals = patch.faceNormals();

            // 计算每个面的角度
            forAll(faceNormals, faceI)
            {
                scalar cosTheta = faceNormals[faceI] & refDir;
                scalar angleDeg = Foam::acos(cosTheta) * 180.0 / M_PI;

                // 写入CSV文件
                outputFile << patch.name() << ","
                           << faceI << ","
                           << angleDeg << endl;
            }
        }
    }

    Info<< "\nResults written to faceAngles.csv" << nl << endl;
    return 0;
}

// ************************************************************************* //
